package com.hsmpay.yl8583.service;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import com.hsmpay.yl8583.utils.Field;
import com.hsmpay.yl8583.utils.Package64;
import com.hsmpay.yl8583.utils.PackageUtil;
import com.hsmpay.yl8583.utils.SpringUtil;
import com.hsmpay.yl8583.utils.TestDes;
import com.hsmpay.utils.DesUtil;
import com.hsmpay.utils.WriteFile;
import com.hsmpay.utils.system.ConConfig;

public class IdeaioeSocketToBoss {
	private ApplicationContext act;
	static WriteFile wf = new WriteFile("ds_Ideaioe");
	public static String debug = ConConfig.getAttribute("IDEAIOE_DAISHOU_DEBUG_STATUS");
	
	 //E点支付生产平台
	static String ipAdd="36.250.6.230";
	static String IPort="48010";
	String itimeout="60000";
	
	String ShopCode = "780421358120001";//商户号
	String PosNo = "35000152";			//终端号
	
	String tpdu = "6000000001";
	String header = "603100310001";
	String batch = "000001"; 			 // 批次号
	String serialNumber = "100007"; 	 // 系统跟踪号(POS中心流水号)
	String operator="001";				 //操作员
	String bit62="H7NL00717784";		//终端序列号的值
	
	//封顶密文密码密钥
	String PinKey="6D46FDC8FE503DF4B980BAEF438776D1";
	//封顶密文报文密钥
	String MacKey="EAF58C6C02AA1E0DEAF58C6C02AA1E0D";
	//磁道密钥
	String trkKey="479602EA3151081204A7862B4A48E928";
	//母POS密钥
	String MasterKey="C7638023E31752D2A1AA262CBCABBF23";
	
	byte[] bitmap = new byte[8];

	static{
		if("1".equals(debug)){
			ipAdd="36.250.6.231";
			IPort="8010";
		}
	}
	
	/**
	 * 构造函数
	 * @param ShopCode
	 * @param PosNo
	 * @param batch
	 * @param MasterKey
	 * @param PinKey
	 * @param MacKey
	 * @param trkKey
	 */
	public IdeaioeSocketToBoss(String ShopCode,String PosNo,String batch,String MasterKey,String PinKey,String MacKey,String trkKey){
		String[] array = PosNo.split(",");
		
		this.ShopCode = ShopCode;
		this.PosNo = array[0];
		this.bit62 = array[1];
		
		this.batch = batch;
		this.MasterKey = MasterKey;
		this.PinKey = PinKey;
		this.MacKey = MacKey;
		this.trkKey = trkKey;
//		if(null != trkKey && StringUtils.isNotEmpty(trkKey.trim()))
//			this.trkKey=DesUtil.decode3DES(trkKey, MasterKey);
		if(null != batch && StringUtils.isEmpty(batch.trim())){
			this.batch = "000001";
		}
		
		act = SpringUtil.getApplicationContext();//new ClassPathXmlApplicationContext("anxinybConfig.xml");
		wf.log("tpdu:" + this.tpdu, "调试");
		wf.log("ShopCode(商户号):" + this.ShopCode, "调试");
		wf.log("PosNo(终端号):" + this.PosNo, "调试");
		wf.log("batch(批次号):" + this.batch, "调试");
		wf.log("MasterKey:" + this.MasterKey, "调试");
		wf.log("PinKey:" + this.PinKey, "调试");
		wf.log("MacKey:" + this.MacKey, "调试");
		wf.log("trkKey:" + this.trkKey,"调试");
	}

	/**
	 * 通信检测(0000)
	 * 
	 * @return 是否正常
	 */
	public String detection(byte[] sendStr) {
		SendSocket send = new SendSocket(ipAdd, IPort, itimeout,wf);
		return send.detection(sendStr);
	}

	public String AddSerialNumber(String SerialNumber) {
		int OldLen = 6;
		String NewStr = (Integer.parseInt(SerialNumber) + 1) + "";
		int NewLen = NewStr.length();
		if (NewLen == 7) {
			NewStr = "000000";
			NewLen = 6;
		}
		for (int i = 0; i < (OldLen - NewLen); i++) {
			NewStr = "0" + NewStr;
		}
		return NewStr;
	}

	public String unAddSerialNumber(String SerialNumber) {
		int OldLen = 6;
		String NewStr = "";
		if (SerialNumber.equals("000000")) {
			NewStr = "999999";
		} else {
			NewStr = (Integer.parseInt(SerialNumber) - 1) + "";
		}
		int NewLen = NewStr.length();
		for (int i = 0; i < (OldLen - NewLen); i++) {
			NewStr = "0" + NewStr;
		}
		return NewStr;
	}

	// 将字符串按指定长度返回,不足位数在前面加0。
	public String GetNewStr(String str, int len) {
		String NewStr = "";
		for (int i = 0; i < len - str.length(); i++) {
			NewStr += "0";
		}
		NewStr += str;
		return NewStr;
	}
	//获取字符串字节长度(双字节长度为2,单字节长度为1)
	public int strlen(String str){ 
		int len=0;
	    try {
			len= (new String(str.getBytes("GBK"), "ISO-8859-1")).length();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		return len;
	}
	//获取字符串中单双字节的个数并以";"分隔返回。
	public String getCharString(String inputText)
	{
		int count = 0; 
		int chCharCount = 0;
		int dconut=0;
		char[] charArray = inputText.toCharArray(); 
		for (int i = 0; i < charArray.length; i++) { 
			char cn = charArray[i]; 
			byte[] bytes = (String.valueOf(cn)).getBytes(); 
			if (bytes.length == 1) { //单字节字符
				dconut++;
				//英文字符 
				//int ascii = bytes[0]; 
			}else if (bytes.length == 2) { //双字节字符 
				count++; 
				int hightByte = 256 + bytes[0]; 
				int lowByte = 256 + bytes[1]; 
				int ascii = (256 * hightByte + lowByte) - 256 * 256; 
				if (ascii >= -20319 && ascii < -10247) { //中文字符 
					chCharCount++; 
				} 
			} 
		} 
		System.out.println( "共有 " + dconut + " 个单字节字符! "); 
		System.out.println( "共有 " + count + " 个双字节字符! "); 
		System.out.println( "共有 " + chCharCount + " 汉字! "); 
		return dconut+";"+count;
	}
	
	/**
	 * 终端签到
	 * 
	 * @return 批次号
	 * @throws Exception
	 */
	public String Login0800(String serialNumber) throws Exception {
		try{
			String LoginMsg = "0";
			String TYPE_ID = "0800";
			wf.log("-------------------请求包域值信息-------------------",	"调试");
			Package64 pk = new Package64();
			pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
			pk.setHeader(PackageUtil.hexStringToByte(this.header));
			pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
			wf.log("[tpdu]=" + this.tpdu, "调试");
			wf.log("[header]=" + this.header, "调试");
			wf.log("[messagetype]=" + TYPE_ID, "调试");
			// 11、41、42、60、63
			String str = "bit11,bit41,bit42,bit60,bit62,bit63";
			for (String one : str.split(",")) {
				Field filed = (Field) act.getBean(one);
				if(one.equals("bit11"))
				{
					filed.valueof(serialNumber);
				}else if (one.equals("bit41")) {
					filed.valueof(this.PosNo);
				}else if (one.equals("bit42")) {
					filed.valueof(this.ShopCode);
				} else if (one.equals("bit60")) {
					filed.valueof("00" + this.batch + "003");
				}else if (one.equals("bit62")) {
					filed.valueof(this.bit62);
				} else if (one.equals("bit63")) {
					filed.valueof(PackageUtil.getCnASCII(this.operator));
				}
				pk.getData().put(filed.getFiledNumber(), filed);
			}
			this.bitmap = Package64.CreateBitMap(pk.getData().keySet());
			wf.log("[bitmap]=" + PackageUtil.byte2hex(this.bitmap),"调试");
			for (Integer one : pk.getData().keySet()) {
				wf.log("bit" + one + ":"+ PackageUtil.byte2hex(pk.getData().get(one).toByte()),"调试");
			}
			wf.log("----------------------------------------------------", "调试");
			pk.setBitmap(this.bitmap);
			byte[] pk_byteArray = pk.package64();
			ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
			buff.putShort((short) pk_byteArray.length);
			buff.put(pk_byteArray);
	
	
			wf.log("请求包:长度 " + pk_byteArray.length + " 签到请求数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
			wf.log("", "调试");
			LoginMsg = detection(buff.array());
			wf.log("响应包:长度 " + LoginMsg.length() / 2 + " 签到响应数据 "+ LoginMsg, "调试");
			if ((!LoginMsg.equals("-1")) && (!LoginMsg.equals(""))) {
				LoginMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(LoginMsg), "39,60,62",wf);
				System.out.println("LoginMsg = "+LoginMsg);
				
				String startMsg = LoginMsg.substring(0, LoginMsg.lastIndexOf("_"));
				String bit62 = LoginMsg.substring(LoginMsg.lastIndexOf("_")+1);
				System.out.println("bit62 = "+bit62);
				//bit62 = PackageUtil.toStringHex(bit62);//转为BCD 16进制
				bit62 = bit62.substring(2, 34)+"_"+bit62.substring(42,58)+bit62.substring(42,58)+"_"+bit62.substring(82,114);
				LoginMsg = startMsg+"_"+bit62;
				
			} else {
				return "-1_";
			}
			wf.log("银联签到返回数据:" + LoginMsg, "调试");
			return LoginMsg;
		}catch(Exception e){
			throw e;
		}
	}
	
	/**
	 * 查询余额
	 * @param trk2
	 * @param trk3
	 * @param pin
	 * @param accountNumber
	 * @param serialNumber
	 * @param bit23
	 * @param bit55
	 * @param bit14
	 * @return
	 * @throws Exception
	 */
	public String Query(String trk2, String trk3, String pin,String accountNumber,String serialNumber,String bit23,String bit55,String bit14)throws Exception {
		try{
			String des_pin=TestDes.MakeDesPin(accountNumber, pin, PinKey,MasterKey);
			
			String QueryMsg = "";
			String TYPE_ID = "0200";
			wf.log("-------------------请求包域值信息-------------------","调试");
			Package64 pk = new Package64();
			pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
			pk.setHeader(PackageUtil.hexStringToByte(this.header));
			pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
			wf.log("[tpdu]=" + this.tpdu, "调试");
			wf.log("[header]=" + this.header, "调试");
			wf.log("[messagetype]=" + TYPE_ID, "调试");
			// 3、11、22、25、26、35、36、41、42、49、52、53、60、64
			String str = "bit3,bit11,bit22,bit25,bit26,bit35,bit36,bit41,bit42,bit49,bit52,bit53,bit60,bit64";
			String temp52=",bit52";
			String temp35=",bit35";
			String temp36=",bit36";
			
			String temp23=",bit23";
			String temp55=",bit55";
			String temp14=",bit14";
			if("".equals(trk2.trim()))
			{
				temp35="";
			}else{
				trk2=DesUtil.ideaioeEncryptStanderTranck(trkKey, trk2,MasterKey);
				System.out.println("密文trk2:"+trk2);
			}
			if("".equals(trk3.trim()))
			{
				temp36="";
			}else{
				trk3=DesUtil.ideaioeEncryptStanderTranck(trkKey, trk3,MasterKey);
			}
			if("".equals(des_pin.trim()))
			{
				temp52="";
			}
			if("".equals(bit23.trim()))
			{
				temp23="";
			}
			if("".equals(bit55.trim()))
			{
				temp55="";
			}
			if("".equals(bit14.trim()))
			{
				temp14="";
			}
			
			str="bit3,bit11"+temp14+",bit22"+temp23+",bit25,bit26"+temp35+temp36+",bit41,bit42,bit49"+temp52+",bit53"+temp55+",bit60,bit64";
			for (String one : str.split(",")) {
				Field filed = (Field) act.getBean(one);
				if (one.equals("bit2")) {
					filed.valueof(accountNumber);
				} else if (one.equals("bit3")) {
					filed.valueof("300000");
				} else if (one.equals("bit11")) {
					filed.valueof(serialNumber);
				} else if (one.equals("bit14")) {
					filed.valueof(bit14);
				} else if (one.equals("bit22")) {
					if("".equals(bit55.trim()))
					{
						filed.valueof("021");
					}else{
						filed.valueof("051");
					}
				} else if (one.equals("bit23")) {
					filed.valueof(bit23);
				} else if (one.equals("bit25")) {
					filed.valueof("00");
				} else if (one.equals("bit26")) {
					filed.valueof("06");
				} else if (one.equals("bit35")) {
					filed.valueof(trk2);
				} else if (one.equals("bit36")) {
					filed.valueof(trk3);
				} else if (one.equals("bit41")) {
					filed.valueof(this.PosNo);
				} else if (one.equals("bit42")) {
					filed.valueof(this.ShopCode);
				} else if (one.equals("bit49")) {
					filed.valueof("156");
				} else if (one.equals("bit52")) {
					byte[] tmp = PackageUtil.hexStringToByte(des_pin);
					filed.valueof(tmp);
				}else if (one.equals("bit53")) {
					filed.valueof("2600000000000000");
				}  else if (one.equals("bit55")) {
					filed.valueof(bit55);
				} else if (one.equals("bit60")) {
					if("".equals(bit55.trim()))
					{
						filed.valueof("01" + this.batch);
					}else{
						filed.valueof("01" + this.batch+"00050");
					}
				}else if (one.equals("bit62")) {
					filed.valueof(this.bit62);
				} else if (one.equals("bit64")) {
					byte[] tmp = PackageUtil.hexStringToByte("4632354542374636");
					filed.valueof(tmp);
				}
				pk.getData().put(filed.getFiledNumber(), filed);
			}
	
			this.bitmap = Package64.CreateBitMap(pk.getData().keySet());
			wf.log("[bitmap]=" + PackageUtil.byte2hex(this.bitmap),"调试");
	
			pk.setBitmap(this.bitmap);
	
			String tranMsg = PackageUtil.byte2hex(pk.getMessagetype())+ PackageUtil.byte2hex(pk.getBitmap());
			for (Integer one : pk.getData().keySet()) {
				if (one != 64) {
					String bitval = PackageUtil.byte2hex(pk.getData().get(one).toByte());
					tranMsg += bitval;
					wf.log("bit" + one + ":" + bitval, "调试");
				}
			}
			String macstr="";
			macstr=TestDes.MakeECBMac(MacKey, tranMsg,MasterKey);
			System.out.println("tranMsg:" + tranMsg);
			System.out.println("bit64:" + macstr);
			wf.log("bit64:" + macstr, "调试");
	
			Field filed = (Field) act.getBean("bit64");
			byte[] tmp = PackageUtil.hexStringToByte(macstr);
			filed.valueof(tmp);
			pk.getData().put(filed.getFiledNumber(), filed);
			byte[] pk_byteArray = pk.package64();
			ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
			System.out.println("pk_byteArray length: " + pk_byteArray.length);
			buff.putShort((short) pk_byteArray.length);
			buff.put(pk_byteArray);
			System.out.println(PackageUtil.byte2hex(buff.array()));
			wf.log(	"----------------------------------------------------", "调试");
			wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
			wf.log("", "调试");
			QueryMsg = detection(buff.array());
			wf.log("响应包:长度 " + QueryMsg.length() / 2 + " 数据 "+ QueryMsg, "调试");
			if ((!QueryMsg.equals("-1")) && (!QueryMsg.equals(""))) {
				QueryMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(QueryMsg), "39,54",wf);
			} else {
				return "-1_";
			}
			wf.log("佘额查询返回数据:" + QueryMsg, "调试");
			return QueryMsg;
		}catch(Exception e){
			throw e;
		}
	}
	
	/**
	 * 帐单费用缴费（交易）
	 * @param trk2 二磁道数据
	 * @param trk3 三磁道数据
	 * @param pin 密码
	 * @param money 交易金额
	 * @param accountNumber 卡号
	 * @param serialNumber 订单号
	 * @param bit23 IC卡序列号
	 * @param bit55 IC卡 55 域
	 * @param bit14 IC卡 序列号
	 * @return 返回消费相关信息
	 * @throws Exception
	 */
	public String BillPayment(String trk2, String trk3, String pin,String money,String accountNumber,String serialNumber,String bit23,String bit55,String bit14) throws Exception {
	//public String BillPayment(String trk2, String trk3, String des_pin,String money,String bit23,String bit55,String bit14) throws Exception {
		try{
			String des_pin=TestDes.MakeDesPin(accountNumber, pin, PinKey,MasterKey);
			
			
			String QueryMsg = "";
			String TYPE_ID = "0200";
			Package64 pk = new Package64();
			pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
			pk.setHeader(PackageUtil.hexStringToByte(this.header));
			pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
			wf.log("-------------------请求包域值信息-------------------",	"调试");
			wf.log("[tpdu]=" + this.tpdu, "调试");
			wf.log("[header]=" + this.header, "调试");
			wf.log("[messagetype]=" + TYPE_ID, "调试");
			// 3、4、11、22、25、26、35、36、41、42、49、52、53、61、64
			String str = "bit3,bit4,bit11,bit22,bit25,bit26,bit35,bit36,bit41,bit42,bit49,bit52,bit53,bit61,bit64";
			String temp52=",bit52";
			String temp35 = ",bit35";
			String temp36 = ",bit36";
			String temp23=",bit23";
			String temp55=",bit55";
			String temp14=",bit14";
			if ("".equals(trk2.trim())) {
				temp35 = "";
			}else{
				trk2=DesUtil.ideaioeEncryptStanderTranck(trkKey, trk2,MasterKey);
			}
			if ("".equals(trk3.trim())) {
				temp36 = "";
			}else{
				trk3=DesUtil.ideaioeEncryptStanderTranck(trkKey, trk3,MasterKey);
			}
			if("".equals(des_pin.trim()))
			{
				temp52="";
			}
			if("".equals(bit23.trim()))
			{
				temp23="";
			}
			if("".equals(bit55.trim()))
			{
				temp55="";
			}
			if("".equals(bit14.trim()))
			{
				temp14="";
			}
			str="bit3,bit4,bit11"+temp14+",bit22"+temp23+",bit25,bit26"+temp35+temp36+",bit41,bit42,bit49"+temp52+",bit53"+temp55+",bit60,bit63,bit64";
			for (String one : str.split(",")) {
				Field filed = (Field) act.getBean(one);
				if (one.equals("bit2")) {
					filed.valueof(accountNumber);
				}else if (one.equals("bit3")) {
					filed.valueof("000000");
				} else if (one.equals("bit4")) {
					filed.valueof(money);
				} else if (one.equals("bit11")) {
					filed.valueof(serialNumber);
				} else if (one.equals("bit14")) {
					filed.valueof(bit14);
				} else if (one.equals("bit22")) {
					if("".equals(bit55.trim()))
					{
						filed.valueof("021");
					}else{
						filed.valueof("051");
					}
				} else if (one.equals("bit23")) {
					filed.valueof(bit23);
				} else if (one.equals("bit25")) {
					filed.valueof("00");
				} else if (one.equals("bit26")) {
					filed.valueof("06");
				} else if (one.equals("bit35")) {
					filed.valueof(trk2);
				} else if (one.equals("bit36")) {
					filed.valueof(trk3);
				} else if (one.equals("bit41")) {
					filed.valueof(this.PosNo);
				} else if (one.equals("bit42")) {
					filed.valueof(this.ShopCode);
				} else if (one.equals("bit49")) {
					filed.valueof("156");
				} else if (one.equals("bit52")) {
					byte[] tmp = PackageUtil.hexStringToByte(des_pin);
					filed.valueof(tmp);
				} else if (one.equals("bit53")) {
					filed.valueof("2600000000000000");
				} else if (one.equals("bit55")) {
					filed.valueof(bit55);
				} else if (one.equals("bit60")) {
					if("".equals(bit55.trim()))
					{
						filed.valueof("22" + this.batch);
					}else{
						filed.valueof("22" + this.batch+"00050");
					}
				} else if (one.equals("bit63")) {
					filed.valueof(PackageUtil.getCnASCII(this.operator));
				} else if (one.equals("bit64")) {
					byte[] tmp = PackageUtil.hexStringToByte("3335344331393934");
					filed.valueof(tmp);
				}
				pk.getData().put(filed.getFiledNumber(), filed);
			}
			pk.setBitmap(Package64.CreateBitMap(pk.getData().keySet()));
	
			String tranMsg = PackageUtil.byte2hex(pk.getMessagetype())
					+ PackageUtil.byte2hex(pk.getBitmap());
			for (Integer one : pk.getData().keySet()) {
				if (one != 64) {
					String bitval = PackageUtil.byte2hex(pk.getData().get(one).toByte());
					tranMsg += bitval;
					wf.log("bit" + one + ":" + bitval, "调试");
				}
			}
			// System.out.println("tranMsg:" + tranMsg);
	
			String macstr="";
			macstr=TestDes.MakeECBMac(MacKey, tranMsg,MasterKey);
			wf.log("bit64:" + macstr, "调试");
			// System.out.println("mac:" + macstr);
			Field filed = (Field) act.getBean("bit64");
			byte[] tmp = PackageUtil.hexStringToByte(macstr);
			filed.valueof(tmp);
			pk.getData().put(filed.getFiledNumber(), filed);
	
			byte[] pk_byteArray = pk.package64();
			ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
			// System.out.println("pk_byteArray length: " + pk_byteArray.length);
			buff.putShort((short) pk_byteArray.length);
			buff.put(pk_byteArray);
			// System.out.println(PackageUtil.byte2hex(buff.array()));
	
			wf.log("----------------------------------------------------", "调试");
			wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "+ PackageUtil.byte2hex(buff.array()), "调试");
			wf.log("", "调试");
			QueryMsg = detection(buff.array());
			wf.log("响应包:长度 " + QueryMsg.length() / 2 + " 数据 "+ QueryMsg, "调试");
			if ((!QueryMsg.equals("-1")) && (!QueryMsg.equals(""))) {
				QueryMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(QueryMsg),"2,4,11,12,13,14,37,38,39",wf);
			} else {
				return "-1_";
			}
			wf.log("帐单费用缴费返回数据:" + QueryMsg, "调试");
			return QueryMsg;
		}catch(Exception e){
			throw e;
		}
	}

	/**
	 * 帐单缴费冲正（交易）
	 * 
	 * @param trk2
	 *            二磁道数据
	 * @param trk3
	 *            三磁道数据
	 * @param des_pin
	 *            卡交易密码
	 * @param money
	 *            交易金额
	 * @param bit61
	 *            61域数据      流水号+批次号+交易日期（0707）
	 * @param bit62
	 *            62域数据      
	 * 
	 * @return 订单信息
	 * @throws Exception
	 */
	public String unBillPayment(String trk2, String trk3,String money,String accountNumber,String serialNumber,String bit37,String bit38,String bit23,String bit55,String bit14,String bit61) throws Exception {
	//public String unBillPayment(String trk2, String trk3,String money,String bit38,String bit23,String bit55,String bit14,String bit61) throws Exception {
		try{
			String QueryMsg = "";
			String TYPE_ID = "0400";
			//this.serialNumber = unAddSerialNumber(this.serialNumber);
			Package64 pk = new Package64();
			pk.setTpdu(PackageUtil.hexStringToByte(this.tpdu));
			pk.setHeader(PackageUtil.hexStringToByte(this.header));
			pk.setMessagetype(PackageUtil.hexStringToByte(TYPE_ID));
			wf.log("-------------------请求包域值信息-------------------","调试");
			wf.log("[tpdu]=" + this.tpdu, "调试");
			wf.log("[header]=" + this.header, "调试");
			wf.log("[messagetype]=" + TYPE_ID, "调试");
			// 冲正39域为冲正原因,取值如下:
			/*
			 * 98 POS终端在时限内未能收到POS中心的应答 96 POS终端收到POS中心的批准应答消息，但由于POS机故障无法完成交易 A0
			 * POS终端对收到POS中心的批准应答消息，验证MAC出错 06 其他情况引发的冲正
			 */
			String str = "bit3,bit4,bit11,bit22,bit25,bit35,bit36,bit38,bit39,bit41,bit42,bit49,bit61,bit62,bit64";
			String temp38 = ",bit38";
			
			String temp23 = ",bit23";
			String temp55 = ",bit55";
			String temp14 = ",bit14";
			String PAN="";
			if(!"".equals(trk2)){
				PAN=trk2.split("d")[0];
				trk2=DesUtil.ideaioeEncryptStanderTranck(trkKey, trk2,MasterKey);
			}
			if(!"".equals(trk3)){
				trk3=DesUtil.ideaioeEncryptStanderTranck(trkKey, trk3,MasterKey);
			}
			if(StringUtils.isNotBlank(bit38))
			{
				temp38="";
			}
			if("".equals(bit23))
			{
				temp23="";
			}
			if("".equals(bit55))
			{
				temp55="";
			}
			if("".equals(bit14))
			{
				temp14="";
			}
			str="bit2,bit3,bit4,bit11"+temp14+",bit22"+temp23+",bit25"+temp38+",bit39,bit41,bit42,bit49"+temp55+",bit60,bit61,bit64";
			for (String one : str.split(",")) {
				Field filed = (Field) act.getBean(one);
				if (one.equals("bit2")) {
					filed.valueof(accountNumber);
				} else if (one.equals("bit3")) {
					filed.valueof("000000");
				} else if (one.equals("bit4")) {
					filed.valueof(money);
				} else if (one.equals("bit11")) {
					filed.valueof(serialNumber);
				} else if (one.equals("bit14")) {
					filed.valueof(bit14);
				} else if (one.equals("bit22")) {
					if("".equals(bit55.trim()))
					{
						filed.valueof("021");
					}else{
						filed.valueof("051");
					}
				} else if (one.equals("bit23")) {
					filed.valueof(bit23);
				} else if (one.equals("bit25")) {
					filed.valueof("00");
				} else if (one.equals("bit35")) {
					filed.valueof(trk2);
				} else if (one.equals("bit36")) {
					filed.valueof(trk3);
				} else if (one.equals("bit38")) {
					filed.valueof(bit38);
				} else if (one.equals("bit39")) {
					filed.valueof("06");
				} else if (one.equals("bit41")) {
					filed.valueof(this.PosNo);
				} else if (one.equals("bit42")) {
					filed.valueof(this.ShopCode);
				} else if (one.equals("bit49")) {
					filed.valueof("156");
				} else if (one.equals("bit55")) {
					filed.valueof(bit55);
				} else if (one.equals("bit60")) {
					if("".equals(bit55.trim()))
					{
						filed.valueof("22" + this.batch);
					}else{
						filed.valueof("22" + this.batch+"00050");
					}
				}else if (one.equals("bit61")) {
					filed.valueof(bit61);
				} else if (one.equals("bit63")) {
					filed.valueof(PackageUtil.getCnASCII(this.operator));
				} else if (one.equals("bit64")) {
					byte[] tmp = PackageUtil.hexStringToByte("0000000000000000");
					filed.valueof(tmp);
				}
				pk.getData().put(filed.getFiledNumber(), filed);
			}
			pk.setBitmap(Package64.CreateBitMap(pk.getData().keySet()));
	
			String tranMsg = PackageUtil.byte2hex(pk.getMessagetype())
					+ PackageUtil.byte2hex(pk.getBitmap());
			for (Integer one : pk.getData().keySet()) {
				if (one != 64) {
					String bitval = PackageUtil.byte2hex(pk.getData().get(one)
							.toByte());
					tranMsg += bitval;
					wf.log("bit" + one + ":" + bitval, "调试");
				}
			}
			System.out.println("tranMsg:" + tranMsg);
	
			String macstr="";
			macstr=TestDes.MakeECBMac(MacKey, tranMsg,MasterKey);
			wf.log("bit64:" + macstr, "调试");
			// System.out.println("mac:" + macstr);
			Field filed = (Field) act.getBean("bit64");
			byte[] tmp = PackageUtil.hexStringToByte(macstr);
			filed.valueof(tmp);
			pk.getData().put(filed.getFiledNumber(), filed);
	
			byte[] pk_byteArray = pk.package64();
			ByteBuffer buff = ByteBuffer.allocate(pk_byteArray.length + 2);
			// System.out.println("pk_byteArray length: " + pk_byteArray.length);
			buff.putShort((short) pk_byteArray.length);
			buff.put(pk_byteArray);
			// System.out.println(PackageUtil.byte2hex(buff.array()));
			wf.log(				"----------------------------------------------------", "调试");
			wf.log("请求包:长度 " + pk_byteArray.length + " 数据 "	+ PackageUtil.byte2hex(buff.array()), "调试");
			wf.log("", "调试");
			
			QueryMsg = detection(buff.array());
			wf.log("响应包:长度 " + QueryMsg.length() / 2 + " 数据 "+ QueryMsg, "调试");
			if ((!QueryMsg.equals("-1")) && (!QueryMsg.equals(""))) {
				QueryMsg = "0"+ Package64.UnPackage(PackageUtil.hexStringToByte(QueryMsg),"2,4,11,12,13,14,37,38,39,44,61",wf);
			} else {
				return "-1_";
			}
			wf.log("帐单缴费冲正返回数据:" + QueryMsg, "调试");
			return QueryMsg;
		}catch(Exception e){
			throw e;
		}
	}
	
	//计算秘钥
	public static void  getMastKey() throws Exception{
		String fl1 = "11C52362A504129B";//分量1
		String fl2 = "2B04D952A812D373";//分量2
		String muKey = fl1+fl2;
		System.out.println(muKey);
		
//		jiemi(muKey,"36313A3FF1DCD1E668B136B3FBFFFFC3","A7195414");
//		jiemi(muKey,"420000CD7744291C9F9D752268980729","C4F942E5");
//		jiemi(muKey,"D65A47CBA63A17243711344D78DE3327","BBD3674B");
//		jiemi(muKey,"B16A97BF675C0FA86846E430AD296C6B","EC1380E3");
//		jiemi(muKey,"E089CCDB1960C3B50710529631756742","C6369CFB");
//		jiemi(muKey,"84DC7E73D6CB42389EBD8C69A73D5B4D","DD482C6D");
//		jiemi(muKey,"A4F8593A1159B14FAABDCDAA27D958E8","5F4827C8");
//		jiemi(muKey,"C8E17C34AEB0BB2C31675AD1A1BA102D","85EBD6A5");
//		jiemi(muKey,"4C150B9FAFCAA822AF4D27992D032EBD","E676DA15");
//		jiemi(muKey,"5ADAC26B182651A7659E86ED05091459","96B6FE90");
//		jiemi(muKey,"5D5A6E2B509817DF88AC7D1FA9704727","775E2A2C");
//		jiemi(muKey,"4370B9F183E89EF31C3C6D2C1DE1AE83","FA260452");
//		jiemi(muKey,"C4754EF567BC23FD5BD6A41492341A9F","8EB2C9EF");
//		jiemi(muKey,"2067D93055DBD7D65CAE2413D142C5B6","7DE0F00C");
//		jiemi(muKey,"548ACEE7B6ACC3275635374F54845393","538F9952");
//		jiemi(muKey,"E02152A068850D289ED5AF7C1A530AF9","6734D8D9");
//		jiemi(muKey,"2E760158503E2917853A4666C33998FC","51EB38C3");
//		jiemi(muKey,"00C20CF9FA46F2C805A942E6F6A5B0EA","53046589");
//		jiemi(muKey,"3835C2AE0C027DDA23655191EA5B575C","D56B33A2");
//		jiemi(muKey,"74E1C741C6BD932665CB2780C177C7C4","0DA6FD52");
//		jiemi(muKey,"D60CA2A23615EAB4596FB95B1E3F1744","F41DE99E");
//		jiemi(muKey,"B579478891027CC966D7C6B59ED6A136","582F8F83");
//		jiemi(muKey,"7F5FAB330DD6F15D389482D72C799AD1","77C7D167");
//		jiemi(muKey,"B9642EDCA40A74455CF2AA0FECCDE587","21E99C57");
//		jiemi(muKey,"1820827CDD0C40A8454E1D6ED783ACC3","9F993ACE");
//		jiemi(muKey,"CBFE13A4DBCEB56A629D3E51F97DC0D4","1D3F0AC5");
//		jiemi(muKey,"9D3F62E182638265FFE59CEE59ADB3B4","DCA91BDF");
//		jiemi(muKey,"0E668E3692588AB244A0579ECEF63863","29F54EE7");
//		jiemi(muKey,"083FDA4B67BA0A407FBF53B43B4D2D4A","713FCCA7");
//		jiemi(muKey,"4082507F9E408C3F01AF012572A040C1","70A10BAD");
//		jiemi(muKey,"BD4922FEF6255613B4DA44E84A79B91B","9F884DE6");
//		jiemi(muKey,"2A8D4611D04DE025AF06C7F13E844B56","DF7AC4C9");
//		jiemi(muKey,"E85D9522D72EAC1FDE16BD2A8C1B12D6","4E79C50E");
//		jiemi(muKey,"514651CC59955D10212FC53B4657E730","A82E059D");
//		jiemi(muKey,"4BE73F0409320CA07F66A40CC4D542AB","E8E13A8E");
//		jiemi(muKey,"C2A8D1BF707120BE4489974B08218315","C13DE4F8");
//		jiemi(muKey,"477AC7C2E9322531F5B19B4FE5A08CF1","E08613DB");
//		jiemi(muKey,"C147B31CB1235DC54C5AF3DAE431FF6E","CF3F70C8");
//		jiemi(muKey,"75787D2B50BBA69D742C83E457E8C7F8","C822E2BD");
//		jiemi(muKey,"C68221D3E0CB6C9B516FC6D60A7B66BA","46664DE9");
//		jiemi(muKey,"687F0FE1AD6259067EB6D0D6A998C193","48E5EBDC");
//		jiemi(muKey,"77BF267747A249DD9E827EF6EB175A73","4D9EAEE2");
//		jiemi(muKey,"4B3B86000E0C78C94E40B20F9E2DFDC9","5B09DA77");
//		jiemi(muKey,"58410A51EA9776F033BD1C8EEC7549F5","189D2343");
//		jiemi(muKey,"4B5BFC8F7F0172C20545788C5841ECBE","F5B41BA1");
//		jiemi(muKey,"1550F68AFCF3C29ACEEF757EAE77DBCA","F3F4423D");
//		jiemi(muKey,"8C7CA3C6E5E7F30EB9453BFFF6687A81","6AD6976B");
//		jiemi(muKey,"6047E151A1A0CFEB188DED06F85E11E9","8BCC57B4");
//		jiemi(muKey,"C2A3F0EB78974E8DA2C750E25CEA3A84","98D266B8");
//		jiemi(muKey,"C8A6D4760ABE4AECD6D7A9EF492799FA","55D82A14");
//		jiemi(muKey,"F41BA61A6C2982BD421DC88E7BAEE81F","E13DFD87");
//		jiemi(muKey,"25B0FDFDA647436E81847A0BCB004359","01299D73");
//		jiemi(muKey,"7DD22BD6517B24232542F4D443423B7C","076630B8");
//		jiemi(muKey,"2C419D6BFEF9E25ABA8394D51CBE6EC1","B10C185D");
//		jiemi(muKey,"6342EAB8B858F9C315AC8D1BD515C1D2","F80A8C76");
//		jiemi(muKey,"68461E6CA53B31B113FF6DC8BD2B00CC","4277BBB9");
//		jiemi(muKey,"0E07FB7933882AF93AB458D085D2B1F1","CE63B81C");
//		jiemi(muKey,"DC1A2AD02145CCBDC9360BF89B4FB3B9","164A1522");
//		jiemi(muKey,"F209112091954A91F738C5291525D50E","BC98BC32");
//		jiemi(muKey,"85F3632E2CA4951423C87A98DD06FFBB","91758252");
//		jiemi(muKey,"51BC002BFEE0D82604318C7234FE04E7","13E5F990");
//		jiemi(muKey,"0429EBB8D5EEAFD9102AFA93EC49FAB2","9F2A3C81");
//		jiemi(muKey,"2B9414A570AE361938D1BE06A15294C8","3061BFFC");
//		jiemi(muKey,"76250FC38050A1D2BC841ED212908081","F5679AA5");
//		jiemi(muKey,"899E86887A7B2924D72A65400DCC140D","8FD4FD6D");
//		jiemi(muKey,"3FD4D5F576D793AB4091E7EB1F928383","F204A5D2");
//		jiemi(muKey,"04C2DFE435DD2305A8C64738A134BF2D","504BA6E7");
//		jiemi(muKey,"B84AEA41D161FC7CBD4F0E5B28F7444A","46096E0D");
//		jiemi(muKey,"7D178727D5EA58976A615B88868F6B92","B8DDAD88");
//		jiemi(muKey,"E2AB5641A09F55C030086921A81C8D8E","6419310C");
//		jiemi(muKey,"4C09D77080C978AB4D8D6FE5BC1877C2","16E4449F");
//		jiemi(muKey,"9CE449F54D9451D0CCB6ADF10674D85F","4A1AA8E2");
//		jiemi(muKey,"CCE7F6E10E62F2099E332524D2CDEA80","0E476F74");
//		jiemi(muKey,"0EC58E43D0A453A53E5694CEC27CF520","779287C5");
//		jiemi(muKey,"AEA7C819875881C7E94A96403ADE8B00","3021929D");
//		jiemi(muKey,"69C7A0B6724B9880C6137CE26E2A9B28","D99BBB36");
//		jiemi(muKey,"2A9FF77C24A07F8D1A24F5EEA5495E0F","5413AE5A");
//		jiemi(muKey,"657DCD8EF0AA0558B38E0E797AA25240","5D3791D8");
//		jiemi(muKey,"E2CA7805F1765F4E5686C6350204B3B5","B3711663");
//		jiemi(muKey,"D3E32BCEA5E30A68B05BF93FCA7F0093","DEAE42A1");
		
//		muKey = "29F21C19BF79AED086F151F220BC8032";
//		jiemi(muKey,"8EC6F65FD53EACE3E93678DD2E0BF848","2A982B60");
//		jiemi(muKey,"6BAB3A7C944B69EC6BAB3A7C944B69EC","1C56DBB6");
//		jiemi(muKey,"68B4EB92053DAC4EC37049F5F88AB723","2B94F158");
		
			
			
			
			
			
			
			
			
			
			
			

		jiemi(muKey,"9CE449F54D9451D0CCB6ADF10674D85F","4A1AA8E2");
		jiemi(muKey,"58410A51EA9776F033BD1C8EEC7549F5","189D2343");
		jiemi(muKey,"4B5BFC8F7F0172C20545788C5841ECBE","F5B41BA1");
		jiemi(muKey,"CCE7F6E10E62F2099E332524D2CDEA80","0E476F74");
		jiemi(muKey,"0EC58E43D0A453A53E5694CEC27CF520","779287C5");
		jiemi(muKey,"AEA7C819875881C7E94A96403ADE8B00","3021929D");
		jiemi(muKey,"69C7A0B6724B9880C6137CE26E2A9B28","D99BBB36");
		jiemi(muKey,"2A9FF77C24A07F8D1A24F5EEA5495E0F","5413AE5A");
		jiemi(muKey,"657DCD8EF0AA0558B38E0E797AA25240","5D3791D8");
		jiemi(muKey,"E2CA7805F1765F4E5686C6350204B3B5","B3711663");
		jiemi(muKey,"D3E32BCEA5E30A68B05BF93FCA7F0093","DEAE42A1");
		jiemi(muKey,"5342D3BB8AC8AE83313AB5D7B1569FE7","6B1BC62D");
		//		System.out.println(MasterKey);
//		System.out.println(MasterKey1);
	}
	
	public static void jiemi(String muKey,String masterKey,String jy)throws Exception{
		String MasterKey1=DesUtil.decode3DES(masterKey,muKey);
		String jy1 = DesUtil.encrypt3DES("0000000000000000", MasterKey1);
		System.out.println(masterKey+"\t"+MasterKey1+"\t"+jy1+"\t"+(jy.equals(jy1.substring(0, 8))));
	}

//	public static void main(String[] args) throws Exception {
//		
////		IdeaioeSocketToBoss st = new IdeaioeSocketToBoss("780110051920001","11000051,JG0000000050","000001","0491D3CD97F807FB627AB3F120F75237","4899AF318C8680FCAF1F4C9D47EC481C","1F0C535D4783489F1F0C535D4783489F","BD550D4A722480E09A74585EE08F5485");
////		IdeaioeSocketToBoss st = new IdeaioeSocketToBoss("780110050440001","11000078,JG0000000079","000001","3ED3D6D0A116C7F8EC91DCBC020B6479","1ED5B857C45A77A99BBA38CC07853DC9","CAA3E4B3866F7BEACAA3E4B3866F7BEA","EEEED12A6F7119FCD02A6CE159DCF861");
////		IdeaioeSocketToBoss st = new IdeaioeSocketToBoss("780110054110001","11000001,JG0000000005","000001","FB5D758CA2A27692A2AD6B58D3B95E34","AA0180C3E4AA1F25EDF012DB37F1A6CD","D4287B855A326434D4287B855A326434","35D998C98A7AEF1494777C87038177CD");
//		IdeaioeSocketToBoss st = new IdeaioeSocketToBoss("780131054110001","11000040,JG0000000039","000001","29F21C19BF79AED086F151F220BC8032","9FE9B9D78F3369083825828C15621FAD","60691401CE07C9D060691401CE07C9D0","A1801B294EB3D6E76A84DC21A37A9963");
//		String serialNumber = "001004";
//		String accountNumber = "6259960005135454";
//		String trk2 = "6259960005135454d21032011442713500000";
//		String trk3 = "";
//		//trk2="6217900100000371909d22092201000065500";
//	//	trk2="6210300016512078d49121206307260000000"; //北京银行
//		String Pin="147258";
//		
//		//0_6210300016512078_000000000001_002004_181331_0514_ _181331647355_ _00
//		//交易金额
//		String money = "000005000001";
//		String bit37="183721652178";
//		String bit38="";
//		String serialNumber2="002005";
//		//IC卡的ID号(32长度,长度不足后补空格)
//		String ICID="";
//		//IC卡编号(4长度,长度不足前补0)
//		String ICNo="";
//		//参数下载类别
//		//		磁条卡交易采用360/361
//		// 		IC卡公钥下载交易采用370/371;
//		//		IC卡参数下载交易采用380/381;
//		//		TMS参数下载结束采用364/365;
//		//		卡BIN黑名单下载结束采用390/391
//		String ParmType="370";
//		String bit23="";
//		String bit55="";
//		String bit14="";
//		//st.serialNumber="200028";
//		//String masKey="AA17070598B13C472F4C502D13F4D6CB";
//		//masKey=DesUtil.decode3DES(masKey,st.MPOSKey);
//		//System.out.println("masKey:"+masKey);
//		
//		
////		System.out.println("终端签到,返回:" + st.Login0800(serialNumber));
////		System.out.println("终端签到,返回:" + st.bit62);
//		
////		System.out.println("银行卡余额查询,返回:" + st.Query(trk2, trk3, Pin,accountNumber,serialNumber,bit23,bit55,bit14));
////		System.out.println("消费,返回:" + st.BillPayment(trk2, trk3, Pin, money,accountNumber,serialNumber,bit23,bit55,bit14));
//	//	System.out.println("冲正,返回:" +st.unBillPayment(trk2, trk3,money,accountNumber,serialNumber, bit37, bit38,bit23,bit55,bit14));	
//	//	System.out.println("消费撤消,返回:"+st.undoBillPayment(trk2, trk3, Pin, money,accountNumber, serialNumber,bit37, bit38, serialNumber2,bit23,bit55,bit14));
//		//System.out.println("IC卡传输密钥下载,返回:"+st.ICTransKeyDown(ICID, ICNo));
//		//需要下载的公钥RID和索引，采用TLV格式，每次只能请求一组公钥
//		//String RIDParm="9F0605A0000003339F220108";
//		//System.out.println("POS参数下载,返回:"+st.POSICParmKeyDown(ParmType,RIDParm));
//		getMastKey();
//		
////		String miKey = DesUtil.decode3DES("188E1E1FA78FDC63D475656E7BD05FEE", "FB5D758CA2A27692A2AD6B58D3B95E34");
////		System.out.println("明文秘钥："+miKey);
//		
////		
////		System.out.println("7BA72A35".equals(DesUtil.encrypt3DES("0000000000000000", a).subSequence(0, 8)));
//		
//	}
}